package com.zac.coroutine.algorithm.leetcode.tree;

/**
 * author zac
 * date 2024/12/2
 */
public class PathSum {



    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        return cal(root, 0, targetSum);
    }

    public boolean cal(TreeNode treeNode, int sum, int target) {
        if (treeNode.left == null && treeNode.right == null) {
            return sum == target;
        }

        sum += treeNode.val;
        if (treeNode.left != null && cal(treeNode.left, sum, target)) {
            return true;
        }
        return treeNode.right != null && cal(treeNode.right, sum, target);
    }

}
